package com.manager.devicemanager.controller;
import com.manager.devicemanager.annotation.OperateLog;
import com.manager.devicemanager.common.OperationTypeEnum;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.entity.DeviceUser;
import com.manager.devicemanager.service.IDeviceInfoService;
import com.manager.devicemanager.vo.DeviceUserVO;
import org.springframework.util.StringUtils;
import com.manager.devicemanager.service.IDeviceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.manager.devicemanager.vo.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 设备用户关联表 前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-05-27
 */
@Tag(name = "DeviceUserController", description = "设备用户关联表 相关接口")
@RestController
@RequestMapping("/deviceUser")
public class DeviceUserController {

    @Autowired
    private IDeviceUserService iDeviceUserService;


    @Operation(summary = "新增")
    @PostMapping("/save")
    public Result<DeviceUser> save(@Parameter(description = "DeviceUser 对象") @RequestBody DeviceUser deviceUser){
       iDeviceUserService.save(deviceUser);
        return Result.success(deviceUser);
    }
    @OperateLog(operationType = OperationTypeEnum.ADD, operationDesc = "设备配置用户")
    @Operation(summary = "新增多个")
    @PostMapping("/saveMany/{deviceid}")
    public Result<String> saveManyUser(@Parameter(description = "设备id") @PathVariable(value = "deviceid") String deviceid, @Parameter(description = "DeviceUser 对象") @RequestBody List<DeviceUser> deviceUsers){
        // 为每个 DeviceUser 对象设置设备 id
        deviceUsers.forEach(user -> {user.setDeviceid(deviceid); user.setId(UUID.randomUUID().toString());});
        iDeviceUserService.remove(new QueryWrapper<DeviceUser>().eq("deviceid", deviceid));
        // 调用服务层的批量保存方法
        iDeviceUserService.saveBatch(deviceUsers);
        return Result.success("成功");
    }

    @Operation(summary = "按id删除")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@Parameter(description = "记录ID", example = "1") @PathVariable("id") String id){
       iDeviceUserService.removeById(id);
        return Result.success(id);
    }

    @Operation(summary = "按id修改")
    @PostMapping("/update/{id}")
    @OperateLog(operationType = OperationTypeEnum.UPDATE, operationDesc = "修改设备用户信息")
    public Result<DeviceUser> update(@Parameter(description = "记录ID", example = "1") @PathVariable("id") String id, @Parameter(description = "DeviceUser 对象") @RequestBody DeviceUser deviceUser){
        deviceUser.setId(id);
       iDeviceUserService.updateById(deviceUser);
        return Result.success(deviceUser);
    }

    @Operation(summary = "按id查询")
    @GetMapping("/get/{id}")
    public Result<DeviceUser> get(@Parameter(description = "记录ID", example = "1") @PathVariable("id") String id){
        DeviceUser deviceUser =iDeviceUserService.getById(id);
        return Result.success(deviceUser);
    }

    @Operation(summary = "条件查询带分页")
    @PostMapping("/page")
    public Result<IPage<DeviceUser>> page(@Parameter(description = "查询条件对象") @RequestBody  PageVO<DeviceUser> deviceUserQuery){
        int pageNum = deviceUserQuery.getPageNum();
        int pageSize = deviceUserQuery.getPageSize();
        DeviceUser deviceUser = deviceUserQuery.getCondition();
         QueryWrapper<DeviceUser> wrapper =new QueryWrapper<>(deviceUser);
        //if (StringUtils.hasText(deviceUser.getName())) {
          //  wrapper.like("name", deviceUser.getName());
        //}
        IPage<DeviceUser> deviceUserList =iDeviceUserService.page(new Page<>(pageNum, pageSize),wrapper );
        return Result.success(deviceUserList);
    }

    @Operation(summary = "条件查询")
    @PostMapping("/list")
    public Result<List<DeviceUser>> list(@Parameter(description = "DeviceUser 查询条件对象") @RequestBody DeviceUser deviceUser){
        List<DeviceUser> deviceUserList =iDeviceUserService.list(new QueryWrapper<>(deviceUser));
        return Result.success(deviceUserList);
    }
    @Operation(summary = "获取设备用户信息")
    @GetMapping("/getDeviceUserInfo/{deviceid}")
    public Result<List<DeviceUserVO>> getDeviceUserInfo(@Parameter(description = "设备id") @PathVariable(value = "deviceid") String deviceid){
        List<DeviceUserVO> deviceUserList =iDeviceUserService.getDeviceUserInfo(deviceid);
        return Result.success(deviceUserList);
    }
}
